//
//  DeviceStatusViewController.m
//  Fantasia
//
//  Created by kongkong on 16/10/1.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "DeviceStatusViewController.h"
#import "DeviceDataCell.h"

@interface DeviceStatusViewController()

@end

@interface DeviceStatusViewController()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong) UIBarButtonItem* modifyButton;
@property (nonatomic,assign) BOOL isModify;
@end

@implementation DeviceStatusViewController

- (instancetype)init{
    self = [super init];
    if (self) {
        self.rightButtons = @[self.modifyButton];
    }
    
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    NSString* title = [[FantasiaStorage instance]memoryValueForKey:@"currentDevice"];
    self.tabBarController.title = [title stringByAppendingString:@" 设备状态"];
    self.autoRefresh = YES;
    [self loadData];
}

- (void)viewWillDisappear:(BOOL)animated{
    self.autoRefresh = NO;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        _tableView.height -= 44;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[DeviceDataCell class] forCellReuseIdentifier:@"cell"];
        
    }
    return _tableView;
}

- (DeviceInfoModel *)model {
    if (!_model) {
        _model = [DeviceStatusModel api:@"device" action:@"FETCHRUNTIME"];
    }
    return _model;
}

- (void)loadData{
    __weak typeof(self) weakSelf = self;
    [self.model relaodData:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf.tableView reloadData];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (strongSelf.autoRefresh) {
                [strongSelf loadData];
            }
        });
    }];
    
}

#pragma -mark tableview

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.model.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DeviceDataCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell reloadWithData:[self.model dataModelWithIndex:indexPath]];
    //cell.textLabel.text = [self.model titleWithIndex:indexPath];
    //cell.detailTextLabel.text = [self.model detailWithIndex:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [DeviceDataCell heightWithData:nil];
}

-(UIBarButtonItem *)modifyButton{
    if (!_modifyButton) {
        _modifyButton = [[UIBarButtonItem alloc]initWithTitle:@"修改" style:UIBarButtonItemStylePlain target:self action:@selector(modifyAction)];
    }
    
    return _modifyButton;
}

- (void)modifyAction{
    if([self.modifyButton.title isEqualToString:@"修改"]){
        self.modifyButton.title = @"完成";
        self.isModify = YES;
        [self.tableView reloadData];
    }
    else{
        self.modifyButton.title = @"修改";
        self.isModify = NO;
        [[NSUserDefaults standardUserDefaults]setValue:self.detailData forKey:[@"detail_data"stringByAppendingString:NSStringFromClass([self class])]];
        [self.tableView reloadData];
    }
}

#pragma -mark 
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag - 10001 < self.detailData.count && textField.tag - 10001 >= 0) {
        textField.text = self.detailData[textField.tag-10001];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.tag - 10001 < self.detailData.count && textField.tag - 10001 >= 0) {
        self.detailData[textField.tag-10001] = textField.text;
    }
    return YES;
}
@end
